package com.songcha.library_common.util

import android.graphics.Paint
import android.graphics.Rect

class FontUtil {
    companion object{
        @JvmStatic
        fun getTextBounds(text:String,textSize:Float,isBold:Boolean,textPaint:Paint?=null): Rect?{
            if(text.isNullOrEmpty()) return null
            if(textPaint!=null){
                val bounds=Rect()
                textPaint.getTextBounds(text,0,text.length,bounds)
                return bounds
            }else{
                val newTextPaint= Paint(Paint.ANTI_ALIAS_FLAG)
                newTextPaint.textSize=textSize
                newTextPaint.isFakeBoldText=isBold
                val bounds=Rect()
                newTextPaint.getTextBounds(text,0,text.length,bounds)
                return bounds
            }

        }

        @JvmStatic
        fun measureText(text:String,textSize:Float,isBold:Boolean,textPaint:Paint?=null):Float{
            if(text.isNullOrEmpty()) return 0f
            if(textPaint != null){
                return textPaint.measureText(text)
            }else{
                val newTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
                newTextPaint.textSize = textSize
                newTextPaint.isFakeBoldText = isBold
                return newTextPaint.measureText(text)
            }
        }

        @JvmStatic
        fun getFontMetrics(textSize:Float,isBold:Boolean,textPaint:Paint?=null): Paint.FontMetrics{
            //if(text.isNullOrEmpty()) return null
            if(textPaint!=null){
                return textPaint.fontMetrics
            }else{
                val newTextPaint= Paint(Paint.ANTI_ALIAS_FLAG)
                newTextPaint.textSize=textSize
                newTextPaint.isFakeBoldText=isBold
                return newTextPaint.fontMetrics
            }
        }
    }
}